/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.machineryrental.service.impl;

import cz.muni.fi.pa165.machineryrental.converters.CustomerDTOConverter;
import cz.muni.fi.pa165.machineryrental.dao.CustomerDAO;
import cz.muni.fi.pa165.machineryrental.dto.CustomerDTO;
import cz.muni.fi.pa165.machineryrental.entities.Customer;
import cz.muni.fi.pa165.machineryrental.service.CustomerService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author jostri
 */
public class CustomerServiceImpl implements CustomerService {

	private CustomerDAO customerDAO;
	
	@Autowired
	private void setCustomerDAO(CustomerDAO customerDAO) {
		if (customerDAO == null) {
			throw new IllegalArgumentException("Given customer is null.");
		}
		
		this.customerDAO = customerDAO;
	}
	
	@Transactional
	public CustomerDTO getById(long id) {
		return CustomerDTOConverter.toDTO(this.customerDAO.getById(id));
	}

	@Transactional
	public void create(CustomerDTO customer) {
		if (customer == null) {
			throw new IllegalArgumentException("Given customer cannot be null");
		}
		
		Customer customerEntity = CustomerDTOConverter.toEntity(customer);
		this.customerDAO.create(customerEntity);
		CustomerDTOConverter.applyToDTO(customer, customerEntity);
	}

	@Transactional
	public void update(CustomerDTO customer) {
		if (customer == null) {
			throw new IllegalArgumentException("Given customer cannot be null");
		}
		
		this.customerDAO.update(CustomerDTOConverter.toEntity(customer));
	}

	@Transactional
	public void remove(CustomerDTO customer) {
		if (customer == null) {
			throw new IllegalArgumentException("Given customer cannot be null");
		}
		
		this.customerDAO.remove(CustomerDTOConverter.toEntity(customer));
	}

	@Transactional
	public List<CustomerDTO> findAll() {
		return CustomerDTOConverter.toDTOList(this.customerDAO.findAll());
	}

	@Transactional
	public List<CustomerDTO> findByLastName(String lastName) {
		return CustomerDTOConverter.toDTOList(this.customerDAO.findByLastName(lastName));
	}

	@Transactional
	public CustomerDTO findByEmail(String email) {
		return CustomerDTOConverter.toDTO(this.customerDAO.findByEmail(email));
	}

	@Transactional
	public CustomerDTO findByCompanyNumber(String companyNumber) {
		return CustomerDTOConverter.toDTO(this.customerDAO.findByCompanyNumber(companyNumber));
	}
	
}
